package com.simple.ds.service.impl;

import com.google.common.collect.Lists;
import com.simple.common.entity.po.RefundTradeEntity;
import com.simple.common.entity.po.TradeEntity;
import com.simple.common.entity.po.sys.RefundTradeInventory;
import com.simple.common.framework.scale.Scale;
import com.simple.ds.service.RefundTradeInventoryApi;
import com.simple.ds.service.RefundTradeService;
import com.simple.ds.service.TradeService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author yasud
 * @date 2021年09月20日 14:11
 * @description
 */
@Service
public class RefundTradeInventoryApiImpl implements RefundTradeInventoryApi {

    @Autowired
    RefundTradeService refundTradeService;

    @Autowired
    TradeService tradeService;

    @Override
    public RefundTradeInventory save_(RefundTradeInventory refundTradeInventory) {
        TradeEntity tradeEntity = tradeService.getById(refundTradeInventory.getRefundTradeId());
        if (Objects.isNull(tradeEntity)) return null;
        RefundTradeEntity refundTradeEntity = new RefundTradeEntity();
        refundTradeEntity.setRefundAmount(Scale.scale(refundTradeInventory.getRefundAmount()));
        refundTradeEntity.setRefundCurrency(tradeEntity.getRealCurrency());
        refundTradeEntity.setRealRefundAmount(Scale.scale(refundTradeInventory.getRealRefundAmount()));
        refundTradeEntity.setRealRefundCurrency(tradeEntity.getRealCurrency());
        refundTradeEntity.setChannelRefundAmount(Scale.scale(refundTradeInventory.getRealRefundAmount()));
        refundTradeEntity.setChannelRefundCurrency(tradeEntity.getRealCurrency());
        refundTradeEntity.setRefundStatus(RefundTradeEntity.Status.valueOf(refundTradeInventory.getStatus().name()));
        refundTradeEntity.setChannelRefundMsg(refundTradeInventory.getChannelRefundMsg());
        refundTradeEntity.setChannelRefundMsg(refundTradeInventory.getChannelRefundStatus());
        refundTradeEntity.setChannelRefundNo(refundTradeInventory.getChannelRefundNo());
        refundTradeEntity.setTradeId(refundTradeInventory.getRefundTradeId());

        refundTradeService.save(refundTradeEntity);
        refundTradeInventory.setId(refundTradeEntity.getId());
        return refundTradeInventory;
    }

    @Override
    public RefundTradeInventory updateById_(RefundTradeInventory refundTradeInventory) {
        TradeEntity tradeEntity = tradeService.getById(refundTradeInventory.getRefundTradeId());
        if (Objects.isNull(tradeEntity)) return null;
        RefundTradeEntity refundTradeEntity = new RefundTradeEntity();
        refundTradeEntity.setRefundAmount(Scale.scale(refundTradeInventory.getRefundAmount()));
        refundTradeEntity.setRefundCurrency(tradeEntity.getRealCurrency());
        refundTradeEntity.setRealRefundAmount(Scale.scale(refundTradeInventory.getRealRefundAmount()));
        refundTradeEntity.setRealRefundCurrency(tradeEntity.getRealCurrency());
        refundTradeEntity.setChannelRefundAmount(Scale.scale(refundTradeInventory.getRealRefundAmount()));
        refundTradeEntity.setChannelRefundCurrency(tradeEntity.getRealCurrency());
        refundTradeEntity.setRefundMsg(refundTradeInventory.getStatus().name());
        refundTradeEntity.setRefundStatus(RefundTradeEntity.Status.valueOf(refundTradeInventory.getStatus().name()));
        refundTradeEntity.setChannelRefundMsg(refundTradeInventory.getChannelRefundMsg());
        refundTradeEntity.setChannelRefundStatus(refundTradeInventory.getChannelRefundStatus());
        refundTradeEntity.setChannelRefundNo(refundTradeInventory.getChannelRefundNo());
        refundTradeEntity.setTradeId(refundTradeInventory.getRefundTradeId());
        refundTradeEntity.setId(refundTradeInventory.getId());
        refundTradeEntity.setMerchantRefundNo(refundTradeInventory.getId() + "");

        refundTradeService.updateById(refundTradeEntity);
        return refundTradeInventory;
    }

    @Override
    public RefundTradeInventory findById(Long refundTradeInventory) {
        RefundTradeEntity refundTradeEntity = refundTradeService.getById(refundTradeInventory);
        return createRefundTradeInventory(refundTradeEntity);
    }

    private RefundTradeInventory createRefundTradeInventory(RefundTradeEntity refundTradeEntity) {
        if (Objects.isNull(refundTradeEntity)) return null;
        RefundTradeInventory inventory = new RefundTradeInventory();
        inventory.setId(refundTradeEntity.getId());
        inventory.setRefundTradeId(refundTradeEntity.getTradeId());
        inventory.setRefundAmount(Scale.scale(refundTradeEntity.getRefundAmount()) + "");
        inventory.setRealRefundAmount(Scale.scale(refundTradeEntity.getRefundAmount()) + "");
        inventory.setChannelRefundMsg(refundTradeEntity.getChannelRefundMsg());
        inventory.setChannelRefundNo(refundTradeEntity.getChannelRefundNo());
        inventory.setChannelRefundStatus(refundTradeEntity.getChannelRefundStatus());
        inventory.setRefundTime(refundTradeEntity.getUpdateTime());
        inventory.setStatus(RefundTradeInventory.Status.valueOf(refundTradeEntity.getRefundStatus().name()));
        inventory.setCreateTime(refundTradeEntity.getCreateTime());
        inventory.setUpdateTime(refundTradeEntity.getUpdateTime());
        return inventory;
    }

    @Override
    public List<RefundTradeInventory> findByChannelRefundNo(String channelRefundTradeNo) {
        List<RefundTradeEntity> refundTradeEntities = refundTradeService.findByChannelRefundNo(channelRefundTradeNo);
        if (CollectionUtils.isNotEmpty(refundTradeEntities)) {
            return refundTradeEntities.stream().map(this::createRefundTradeInventory).collect(Collectors.toList());
        }
        return Lists.newArrayList();
    }
}
